package leetcode.L322;

import java.util.Arrays;

class Solution {
    public int coinChange(int[] coins, int amount) {
        //dp[i]表示凑成i需要的最少硬币数
        int[] dp = new int[amount + 1];
        for (int j = 1; j <= amount; j++) {
            dp[j] = amount + 1;
        }
        dp[0] = 0;
        for (int j = 1; j <= amount; j++) {
            for (int coin : coins) {
                if (j >= coin) {
                    dp[j] = Math.min(dp[j - coin] + 1, dp[j]);
                }
            }
        }

        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }

    public int coinChange1(int[] coins, int amount) {
        // dp[i] 表示凑成 i 需要的最少硬币数
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1); // 初始化为一个较大的值，表示无法凑成
        dp[0] = 0; // 凑成金额 0 不需要任何硬币

        for (int i = 1; i <= amount; i++) {
            for (int coin : coins) {
                if (i >= coin) {
                    dp[i] = Math.min(dp[i], dp[i - coin] + 1);
                }
            }
        }

        return dp[amount] > amount ? -1 : dp[amount];
    }
}